
# 单例模式代码示例
class Singleton(object):
    def __new__(cls, *args, **kwargs):
        if not hasattr(Singleton, "_instance"):
            Singleton._init_flag = True
            Singleton._instance = super().__new__(cls)
        return Singleton._instance

    def __init__(self, name):
        if not hasattr(Singleton, "_init"):
            Singleton._init = True
            print(">>> 初始化 <<<")
            self.name = name

    def print_name(self):
        print(self.name)


if __name__ == '__main__':
    s1 = Singleton('s1')
    s2 = Singleton('s2')
    print(s1, s2)
    print(s1 is s2)
    s1.print_name()
    s2.print_name()
